import ConstDef from '@/js/ConstDef'
import partyService from '@/api/party/PartyService'
import { ElMessage } from 'element-plus'
import cacheManage from '@/cache/CacheManage'
import ExtBase from "@/views/basic/ext/ExtBase";

class PartyEntryExt extends ExtBase {
  hasAPIMethod(apiName) {
    if (apiName === 'saveNvs') {
      return true;
    } else if (apiName === 'getNv') {
      return true;
    } else return false;
  }

  getAPIMethod(apiName) {
    if (apiName === 'saveNvs') {
      return {
        func: partyService.savePartyNvs,
        obj: partyService
      };
    } else if (apiName === 'getNv') {
      return {
        func: this.getPartyNv,
        obj: this
      }
    } else {
      return null;
    }
  }

  getPartyNv(commonInfo, ndCode, nvId) {
    return partyService.getPartyNv(commonInfo, nvId);
  }

  interceptEvent (functionCode) {
    if (functionCode === ConstDef.FUNCTION_CODE_SAVE ||
    functionCode === ConstDef.FUNCTION_CODE_COMMIT) {
      return true;
    } else {
      return false;
    }
  }

  supportEvent (functionCode) {
    return false;
  }

  async handleEvent(self, event) {
    const fvData = event.fvData;
    const fdLayout = fvData.fdLayout;
    const functionCode = fdLayout.functionCode;
    if (functionCode === ConstDef.FUNCTION_CODE_SAVE) {
      this.saveNv(self, event)
    } else if (functionCode === ConstDef.FUNCTION_CODE_COMMIT) {
      this.saveNv(self, event);
    }
  }

  async saveNv(self, event) {
    const commonInfo = cacheManage.getDefaultCommonInfo();
    const nvs = self.nvsData.getNvs();
    const nvMsg = await partyService.savePartyNvs(commonInfo, nvs);
    if (!nvMsg.success) return;
    ElMessage({
      message: '保存成功!',
      type: 'success',
      plain: true
    })
    self.nvsData.nvs = nvMsg.value;
  }
}

const partyEntryExt = new PartyEntryExt();
export default partyEntryExt;
